package pers.vic.practice.module.student.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import pers.vic.boot.base.controller.BaseController;

import pers.vic.boot.base.model.BaseResponse;
import pers.vic.boot.base.vo.Select2VO;
import pers.vic.practice.module.student.model.SClasses;
import pers.vic.practice.module.student.model.SCourse;
import pers.vic.practice.module.student.service.SClassesService;
import pers.vic.practice.module.student.service.SStudentService;
import pers.vic.practice.module.student.model.SStudent;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:学生表 控制层
 * @author Vic.xu
 * @date: 2022-04-12 09:56
 */
@RestController
@RequestMapping("/student/student")
public class SStudentController extends BaseController<SStudentService, SStudent>{

    @Resource
    private SClassesService classesService;


    @GetMapping("/home")
    public ModelAndView home(){
        ModelAndView modelAndView = new ModelAndView("student/student/home");
        bindSelectInfo(modelAndView);
        return modelAndView;
    }

    @GetMapping("/form")
    public ModelAndView form(Integer id){
        ModelAndView modelAndView = new ModelAndView("student/student/form");
        bindSelectInfo(modelAndView);
        if (id != null) {
            modelAndView.addObject("entity", service.findById(id));
        }
        return modelAndView;
    }

    private void bindSelectInfo(ModelAndView modelAndView){
        List<Select2VO> classes = classesService.list(new SClasses()).stream().map(s -> new Select2VO(s.getId(), s.getName())).collect(Collectors.toList());
        modelAndView.addObject("classes", classes);
    }

    @PostMapping(value = "/checkNumber")
    public BaseResponse<?> checkNumber(Integer id, String number) {
        boolean ok = checkNotRepeat(id, number, "number");
        return BaseResponse.success(ok);
    }
}
